package com.weipeep.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.weipeep.common.mail.EmailService;
import com.weipeep.common.utils.IdGenerator;
import com.weipeep.sys.dao.DemandDao;
import com.weipeep.sys.dao.WebUserDao;
import com.weipeep.sys.dao.XunJiaDao;
import com.weipeep.sys.domain.DemandDO;
import com.weipeep.sys.domain.WebUserDO;
import com.weipeep.sys.domain.XunJiaDO;
import com.weipeep.sys.vo.BaojiaVo;
import com.weipeep.sys.vo.ReceiveBaojiaQuery;
import org.springframework.stereotype.Service;

import com.weipeep.sys.dao.BaoJiaDao;
import com.weipeep.sys.domain.BaoJiaDO;
import com.weipeep.sys.service.BaoJiaService;
import com.weipeep.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-21 00:42:43 | wangyun</small>
 */
@Service
public class BaoJiaServiceImpl extends CoreServiceImpl<BaoJiaDao, BaoJiaDO> implements BaoJiaService {
    @Resource
    private IdGenerator idGenerator;
    @Resource
    private DemandDao demandDao;
    @Resource
    private BaoJiaDao baoJiaDao;
    @Resource
    private XunJiaDao xunJiaDao;
    @Resource
    private WebUserDao webUserDao;
    @Resource
    private EmailService emailService;
    @Override
    @Transactional
    public boolean baojia(BaoJiaDO baoJia) {
        Long baojiaId = idGenerator.nextId();
        Long xunjiaId = null;
        boolean flag = false;
        Long emailToUserId = null;
        if(baoJia.getXunJiaId()==null){
            DemandDO demand = demandDao.selectById(baoJia.getDemandId());
            emailToUserId = demand.getUserId();
            xunjiaId = idGenerator.nextId();
            XunJiaDO xunJiaDO = new XunJiaDO();
            xunJiaDO.setId(xunjiaId);
            xunJiaDO.setDemandId(baoJia.getDemandId());
            xunJiaDO.setModel(demand.getModel());
            xunJiaDO.setBrand(demand.getBrand());
            xunJiaDO.setType(demand.getType());
            xunJiaDO.setNumber(demand.getNumber());
            xunJiaDO.setUserId(demand.getUserId());
            xunJiaDO.setToUserId(baoJia.getUserId());
            xunJiaDO.setCreateTime(Calendar.getInstance().getTime());
            xunJiaDO.setXunJiaType(2);//对demand生成询价
            baoJia.setId(baojiaId);
            baoJia.setXunJiaId(xunjiaId);
            baoJia.setCreateTime(Calendar.getInstance().getTime());
            flag = baoJiaDao.insert(baoJia)==1 &&xunJiaDao.insert(xunJiaDO)==1;
        }else {
            xunjiaId = baoJia.getXunJiaId();
            XunJiaDO xunJiaDO = xunJiaDao.selectById(xunjiaId);
            emailToUserId =xunJiaDO.getUserId();
            baoJia.setId(baojiaId);
            baoJia.setXunJiaId(xunjiaId);
            baoJia.setCreateTime(Calendar.getInstance().getTime());
            flag = baoJiaDao.insert(baoJia)==1;
        }
        final Long userId = emailToUserId;
        final String subject = "收到报价";
        final StringBuilder sb = new StringBuilder();
        WebUserDO webUserDO = webUserDao.selectById(baoJia.getUserId());
        sb.append("<html>");
        sb.append("<body>");
        sb.append("<div>您好,这是由一号轴承网www.01bearing.com接到的报价单，请即登录www.01bearing.com回复处理！祝您成功！</div>");
        sb.append("<div>以下是供货商的报价信息:</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;品牌："+(baoJia.getBrand()==null?"":baoJia.getBrand())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;类型："+(baoJia.getType()==null?"":baoJia.getType())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;型号："+(baoJia.getModel()==null?"":baoJia.getModel())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;数量："+(baoJia.getNumber()==null?"":baoJia.getNumber())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;单价："+(baoJia.getUnitPrice()==null?"":baoJia.getUnitPrice())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;备注："+(baoJia.getNote()==null?"":baoJia.getNote())+"</div>");
        sb.append("<div>此报价来自:</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;姓名："+(webUserDO.getUserName()==null?"":webUserDO.getUserName())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;公司名称："+(webUserDO.getCompany()==null?"":webUserDO.getCompany())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;电话："+(webUserDO.getTelephone()==null?"":webUserDO.getTelephone())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;传真："+(webUserDO.getFaxs()==null?"":webUserDO.getFaxs())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;手机："+(webUserDO.getMobile()==null?"":webUserDO.getMobile())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;Email："+(webUserDO.getEmail()==null?"":webUserDO.getEmail())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;QQ："+(webUserDO.getQq()==null?"":webUserDO.getQq())+"</div>");
        sb.append("<div>&nbsp;&nbsp;&nbsp;&nbsp;地址："+(webUserDO.getAddress()==null?"":webUserDO.getAddress())+"</div>");
        sb.append("<div>此信由一号轴承网www.01bearing.com专业轴承交易平台实时发送,感谢您选择我们的服务!</div>");
        sb.append("</body>");
        sb.append("</html>");
        new Thread(){
            @Override
            public void run() {
                WebUserDO userDO = webUserDao.selectById(userId);
                if (userDO.getEmail()!=null){
                    emailService.sendHtmlMail(userDO.getEmail(),subject,sb.toString());
                }
            }
        }.start();
        return flag;
    }

    @Override
    public List<BaojiaVo> receiveBaojia(ReceiveBaojiaQuery query) {
        return baoJiaDao.fetchReceiveBaojia(query);
    }

    @Override
    public int receiveBaojiaCount(ReceiveBaojiaQuery query) {
        return baoJiaDao.fetchReceiveBaojiaCount(query);
    }

    @Override
    public List<BaojiaVo> myBaojia(ReceiveBaojiaQuery query) {
        return baoJiaDao.fetchMyBaojia(query);
    }

    @Override
    public int myBaojiaCount(ReceiveBaojiaQuery query) {
        return baoJiaDao.fetchMyBaojiaCount(query);
    }
}
